package com.open.capacity.prints.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.prints.dao.BillSfpzDao;
import com.open.capacity.prints.entity.BillSfpz;
import com.open.capacity.prints.service.BillSfpzService;
import com.open.capacity.prints.util.ChineseYuanUtil;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.BigDecimalUtil;
import com.open.capacity.utils.DateUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class BillSfpzServiceImpl implements BillSfpzService {

    @Autowired
    private BillSfpzDao billSfpzDao;

    /**
     * 添加
     *
     * @param billSfpz
     */
    public String save(BillSfpz billSfpz) {
        billSfpz.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        billSfpz.setCreateTime(new Date());
        Calendar calendar = Calendar.getInstance();

        if(billSfpz.getDjrq() != ""){
            calendar.setTime(DateUtil.parseDate2(billSfpz.getDjrq().replaceAll("-", "")
                    .replaceAll(" ", "")));//放入Date类型数据

            billSfpz.setYear(calendar.get(Calendar.YEAR));//获取年份
            billSfpz.setMonth(String.valueOf(calendar.get(Calendar.MONTH) + 1));//获取月份
            billSfpz.setDay(String.valueOf(calendar.get(Calendar.DATE)));//获取日
            billSfpz.setDjrq(billSfpz.getDjrq().replaceAll("-", "").replaceAll(" ", ""));
        }
        BigDecimal money = billSfpz.getCost1()
                .add(BigDecimalUtil.getData(billSfpz.getCost2()))
                .add(BigDecimalUtil.getData(billSfpz.getCost3()))
                .add(BigDecimalUtil.getData(billSfpz.getCharge1()))
                .add(BigDecimalUtil.getData(billSfpz.getCharge2()))
                .add(BigDecimalUtil.getData(billSfpz.getCharge3()))
                .add(BigDecimalUtil.getData(billSfpz.getYdcost1()))
                .add(BigDecimalUtil.getData(billSfpz.getYdcost2()))
                .add(BigDecimalUtil.getData(billSfpz.getYdcost3()));
        billSfpz.setMoney2(money);

        billSfpz.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billSfpz.getMoney2()));
        billSfpzDao.save(billSfpz);
        return billSfpz.getId();
    }

    /**
     * 修改
     *
     * @param billSfpz
     */
    public String update(BillSfpz billSfpz) {
        billSfpz.setCreateTime(new Date());
        Calendar calendar = Calendar.getInstance();
        if(billSfpz.getDjrq() != ""){
            calendar.setTime(DateUtil.parseDate2(billSfpz.getDjrq().replaceAll("-", "")
                    .replaceAll(" ", "")));//放入Date类型数据

            billSfpz.setYear(calendar.get(Calendar.YEAR));//获取年份
            billSfpz.setMonth(String.valueOf(calendar.get(Calendar.MONTH) + 1));//获取月份
            billSfpz.setDay(String.valueOf(calendar.get(Calendar.DATE)));//获取日
            billSfpz.setDjrq(billSfpz.getDjrq().replaceAll("-", "").replaceAll(" ", ""));
        }
//        calendar.setTime(DateUtil.parseDate2(billSfpz.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
//        billSfpz.setYear(calendar.get(Calendar.YEAR));                    //获取年份
//        billSfpz.setMonth(String.valueOf(calendar.get(Calendar.MONTH) + 1));                    //获取月份
//        billSfpz.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日

        BigDecimal money = billSfpz.getCost1()
                .add(BigDecimalUtil.getData(billSfpz.getCost2()))
                .add(BigDecimalUtil.getData(billSfpz.getCost3()))
                .add(BigDecimalUtil.getData(billSfpz.getCharge1()))
                .add(BigDecimalUtil.getData(billSfpz.getCharge2()))
                .add(BigDecimalUtil.getData(billSfpz.getCharge3()))
                .add(BigDecimalUtil.getData(billSfpz.getYdcost1()))
                .add(BigDecimalUtil.getData(billSfpz.getYdcost2()))
                .add(BigDecimalUtil.getData(billSfpz.getYdcost3()));
        billSfpz.setMoney2(money);

        billSfpz.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billSfpz.getMoney2()));
        billSfpzDao.update(billSfpz);
        return billSfpz.getId();
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return billSfpzDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BillSfpz findById(String id) {
        BillSfpz item = billSfpzDao.findById(id);
        //总额
        BigDecimal resut = item.getMoney2();
        Integer million5Count = resut.divide(BigDecimal.valueOf(100000000)).intValue();
        item.setMillion5(million5Count);
        resut = resut.subtract(BigDecimal.valueOf(million5Count * 100000000));

        Integer million4Count = resut.divide(BigDecimal.valueOf(10000000)).intValue();
        item.setMillion4(million4Count);
        resut = resut.subtract(BigDecimal.valueOf(million4Count * 10000000));


        Integer million3Count = resut.divide(BigDecimal.valueOf(1000000)).intValue();
        item.setMillion3(million3Count);
        resut = resut.subtract(BigDecimal.valueOf(million3Count * 1000000));

        Integer million2Count = resut.divide(BigDecimal.valueOf(100000)).intValue();
        item.setMillion2(million2Count);
        resut = resut.subtract(BigDecimal.valueOf(million2Count * 100000));

        Integer million1Count = resut.divide(BigDecimal.valueOf(10000)).intValue();
        item.setMillion1(million1Count);
        resut = resut.subtract(BigDecimal.valueOf(million1Count * 10000));

        Integer thousandCount = resut.divide(BigDecimal.valueOf(1000)).intValue();
        item.setThousand(thousandCount);
        resut = resut.subtract(BigDecimal.valueOf(thousandCount * 1000));

        Integer hundredCount = resut.divide(BigDecimal.valueOf(100)).intValue();
        item.setHundred(hundredCount);
        resut = resut.subtract(BigDecimal.valueOf(hundredCount * 100));

        Integer tenCount = resut.divide(BigDecimal.valueOf(10)).intValue();
        item.setTen(tenCount);
        resut = resut.subtract(BigDecimal.valueOf(tenCount * 10));

        Integer yuanCount = resut.divide(BigDecimal.valueOf(1)).intValue();
        item.setYuan(yuanCount);
        resut = resut.subtract(BigDecimal.valueOf(yuanCount * 1));

        Integer jiaoCount = resut.divide(BigDecimal.valueOf(0.1)).intValue();
        item.setJiao(jiaoCount);
        resut = resut.subtract(BigDecimal.valueOf(jiaoCount * 0.1));

        Integer fenCount = resut.divide(BigDecimal.valueOf(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue();
        item.setFen(fenCount);

        item.setCostTotal(BigDecimalUtil.getData(item.getCost1()).add(BigDecimalUtil.getData(item.getCost2())).add(BigDecimalUtil.getData(item.getCost3())));
        item.setChargeTotal(BigDecimalUtil.getData(item.getCharge1()).add(BigDecimalUtil.getData(item.getCharge2())).add(BigDecimalUtil.getData(item.getCharge3())));
        item.setYdcostTotal(BigDecimalUtil.getData(item.getYdcost1()).add(BigDecimalUtil.getData(item.getYdcost2())).add(BigDecimalUtil.getData(item.getYdcost3())));
        return item;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BillSfpz> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillSfpz> list = billSfpzDao.findAll(params);
        PageInfo<BillSfpz> pageInfo = new PageInfo(list);

        return PageResult.<BillSfpz>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BillSfpz> findAll(Map<String, Object> params) {
        return billSfpzDao.findAll(params);
    }

    /**
     * 查询记录
     *
     * @param hm
     * @return
     */
    public List<BillSfpz> findByHm(String hm) {
        return billSfpzDao.findByHm(hm);
    }

}
